﻿namespace ExamV5.GradeInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmShowMXDay : Page
    {
        protected Button butExp;
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected Label labTip;
        protected Label labUserInfo;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        private string string_0 = "";
        private string string_1 = "";
        protected TextBox txtEndDate;
        protected TextBox txtStartDate;
        protected UpdatePanel UPPart;

        protected void butExp_Click(object sender, EventArgs e)
        {
            try
            {
                string cmdText = "";
                string path = base.Server.MapPath("../") + @"\PublicFile\MXDayRFen" + this.httpCookie_3.Value + ".csv";
                string str3 = "";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                StreamWriter writer = new StreamWriter(path, true, Encoding.GetEncoding("GB2312"));
                writer.WriteLine("准考证号,考生姓名,科目名称,做答试题,做答结果,做答时间");
                cmdText = "select a.ID,a.RubricID,a.IsOK,c.SubjectName,b.RubricTitle,a.InputTime from DayRecored a,RubricInfo b,SubjectInfo c where b.ID=a.RubricID and c.ID=b.SubjectID and a.UserCode='" + this.string_0 + "'";
                if (this.txtStartDate.Text.Trim() != "")
                {
                    cmdText = cmdText + " and Convert(dateTime,a.InputTime)>=Convert(dateTime,'" + this.publicFun_0.CheckString(this.txtStartDate.Text.Trim()) + " 00:00:00')";
                }
                if (this.txtEndDate.Text.Trim() != "")
                {
                    cmdText = cmdText + " and Convert(dateTime,'" + this.publicFun_0.CheckString(this.txtEndDate.Text.Trim()) + " 23:59:59')>=Convert(dateTime,a.InputTime)";
                }
                cmdText = cmdText + "  order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                connection.Open();
                SqlCommand command = new SqlCommand(cmdText, connection);
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    str3 = "";
                    str3 = (((((str3 + this.string_0 + "\x00b7,") + this.string_1 + ",") + reader["SubjectName"].ToString().Trim() + ",") + reader["RubricTitle"].ToString().Trim().Replace(",", "，") + ",") + reader["IsOK"].ToString().Trim() + ",") + reader["InputTime"].ToString().Trim() + ",";
                    writer.WriteLine(str3);
                }
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
                writer.Flush();
                writer.Close();
                this.labTip.Text = " 导出成功！单击【<a href='../FrmDownLoad.aspx?FileName=MXDayRFen" + this.httpCookie_3.Value + ".csv' target=_blank><font color=red>下载</font></a>】进行保存";
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BExpErrTip", @"alert('导出数据失败！\n\n请将PublicFile目录设置为Everyone用户可修改权限！')", true);
            }
        }

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) != 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
            }
            else
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = this.publicFun_0.RemoveHTML(e.Row.Cells[1].Text).Replace("&lt;BR&gt;", "").Replace("&lt;br&gt;", "").Replace("&nbsp;", "");
                if (e.Row.Cells[1].Text.Length > (Convert.ToInt32(this.ViewState["SubStrLen"].ToString()) + 10))
                {
                    e.Row.Cells[1].Text = "<a title=" + e.Row.Cells[1].Text + ">" + e.Row.Cells[1].Text.Substring(0, Convert.ToInt32(this.ViewState["SubStrLen"].ToString()) + 10) + "…</a>";
                }
                if (e.Row.Cells[0].Text.Length > 12)
                {
                    e.Row.Cells[0].Text = "<a title=" + e.Row.Cells[0].Text + ">" + e.Row.Cells[0].Text.Substring(0, 12) + "…</a>";
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (!(this.ViewState["SortType"].ToString() == " Desc"))
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "InputTime":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "IsOK":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            return;
                    }
                    e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                }
                else
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "InputTime":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "IsOK":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                            return;
                    }
                    e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                }
            }
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (!(this.ViewState["SortFiled"].ToString() == sortExpression))
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            else if (!(this.ViewState["SortType"].ToString() == " Desc"))
            {
                this.ViewState["SortType"] = " Desc";
            }
            else
            {
                this.ViewState["SortType"] = " ASC";
            }
            this.method_0();
        }

        protected void DDLDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        private void method_0()
        {
            string selectCommandText = "";
            selectCommandText = "select a.ID,a.RubricID,a.IsOK,c.SubjectName,b.RubricTitle,a.InputTime from DayRecored a,RubricInfo b,SubjectInfo c where b.ID=a.RubricID and c.ID=b.SubjectID and a.UserCode='" + this.string_0 + "'";
            if (this.txtStartDate.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and Convert(dateTime,a.InputTime)>=Convert(dateTime,'" + this.publicFun_0.CheckString(this.txtStartDate.Text.Trim()) + " 00:00:00')";
            }
            if (this.txtEndDate.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and Convert(dateTime,'" + this.publicFun_0.CheckString(this.txtEndDate.Text.Trim()) + " 23:59:59')>=Convert(dateTime,a.InputTime)";
            }
            selectCommandText = selectCommandText + "  order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.string_0 = base.Server.UrlDecode(base.Request["UserCode"].ToString());
                this.string_1 = base.Server.UrlDecode(base.Request["UserName"].ToString());
                this.labUserInfo.Text = "查看〖" + this.string_1 + "(" + this.string_0 + ")〗每日一题做答明细列表";
                this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString + "--查看每日一题做答明细列表";
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        if (this.httpCookie_2.Value == "A")
                        {
                            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckA", "alert('成绩查询权限未开放！')", true);
                        }
                        else
                        {
                            this.ViewState["SortFiled"] = "InputTime";
                            this.ViewState["SortType"] = " Desc";
                            string str = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='SubStrLength'", "ParmValue").Trim();
                            if (str.Trim() == "")
                            {
                                str = "20";
                            }
                            this.ViewState["SubStrLen"] = str;
                            this.method_0();
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

